package cn.com.gas.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.util.HmacSha256Utils;
import cn.com.util.MD5Utils;
import cn.com.util.TsBqUtils;
import io.swagger.annotations.ApiOperation;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.util.Base64;


/**
 * 实时视频
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-03-22 11:21:42
 */
@Controller
@RequestMapping("/gas/realvideo")
public class GasRealvideoController {

	@Value("${gasstation.videosQueryVideoGetLivePlayUrl}")
	private String url;

	@Value("${gasstation.thirdPartyPass}")
	private String pass;
	@Value("${gasstation.thirdPartyUser}")
	private String user;
	
	@ApiOperation(value = "获取实时视频url")
	@PostMapping("/geturl")
	@ResponseBody
	public DataResult getUrl(@RequestBody GasStationinfoEntity gasStationinfo) throws UnsupportedEncodingException {

		if (TsBqUtils.isNull(gasStationinfo.getId())) {
			return DataResult.fail("摄像机编号为空！请稍后重试");
		}
		String request ="/Videos/QueryVideo/GetLivePlayUrl?api=admin&cameraId=" + gasStationinfo.getId() + "&stream=1";
		String sign = sign(request);
		return DataResult.success(url + "?api=admin&cameraId=" + gasStationinfo.getId() + "&stream=1&sign=" + sign);
	}

	private String sign(String url) throws UnsupportedEncodingException {

		String encrypt = MD5Utils.encrypt(pass);
		System.out.println(encrypt);
		byte[] hmacSHA256 = HmacSha256Utils.hmacSHA256(url, encrypt);
		System.out.println(hmacSHA256);
		byte[] encodedBytes = Base64.getEncoder().encode(hmacSHA256);
		System.out.println(new String(encodedBytes));

		String encodedString = URLEncoder.encode(new String(encodedBytes), "UTF-8");
		System.out.println(encodedString);

		return encodedString;
	}

	public String removeFirstAndLastChar(String s) {
		if (s.length() <= 2) {
			return ""; // 如果字符串长度小于等于2，则返回空字符串
		}
		String str = s.substring(1, s.length() - 1);
		String replaceAll = str.replaceAll("\\\\", "");
		return replaceAll;

	}

}
